import { _decorator, gfx, postProcess, Material, EffectAsset, renderer, rendering, Vec4, Camera, CachedArray, Sprite } from 'cc';
const { Format } = gfx

const { ccclass, property, menu, executeInEditMode } = _decorator;

/**
 * 
 * 就是一个普通的组件 自定义后处理节点的资源和行为
 * 
 */
@ccclass('GaussianBlur')
@menu('PostProcess/GaussianBlur')
@executeInEditMode
export class GaussianBlur extends postProcess.PostProcessSetting {

    @property(Camera)
    mainCamera: Camera = null;

    @property(Sprite)
    preview: Sprite = null;

    @property(Boolean)
    clearBack: boolean = true;

    @property(EffectAsset)
    _effectAsset: EffectAsset | undefined

    public previewRenderTexture = null;

    @property(EffectAsset)
    get effect() {
        return this._effectAsset;
    }
    set effect(v) {
        this._effectAsset = v;
        if (this._effectAsset == null) {
            this._material = null;
        }
        else {
            if (this._material == null) {
                this._material = new Material();
            }
            this._material.reset({ effectAsset: this._effectAsset });
        }
        this.updateMaterial();
    }

    @property
    iterations = 3;

    @property
    get blurRadius() {
        return this._blurParams.x;
    }
    set blurRadius(v) {
        this._blurParams.x = v;
        this.updateMaterial();
    }


    private _material: Material;
    public get material(): Material {
        return this._material;
    }

    @property
    private _blurParams: Vec4 = new Vec4(1.0, 0.0, 0.0, 0.0);
    public get blurParams(): Vec4 {
        return this._blurParams;
    }

    updateMaterial() {
        if (!this._material) {
            return;
        }
        this._material.setProperty('blurParams', this.blurParams);
    }

    protected start(): void {
        if (this._effectAsset) {
            this._material = new Material();
            this._material.initialize({ effectAsset: this._effectAsset });

            this._material.setProperty('blurParams', this.blurParams);
        }
    }
}